<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/common/taglibs.jsp"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>添加部门</title>
		<link rel="stylesheet" type="text/css" href="${ctx }/css/caas_style.css" />
		<script type="text/javascript" src="${ctx }/js/jquery/jquery-1.3.min.js"></script>
		<script type="text/javascript" src="${ctx}js/jquery/lhgdialog/lhgcore.min.js"></script>
		<script type="text/javascript" src="${ctx}js/jquery/lhgdialog/lhgdialog.min.js"></script>
		<SCRIPT language=JavaScript>  
			jQuery(document).ready(function(){
				initUserType();
							
			    var dg = frameElement.lhgDG; 			   
			   			   
			    var p = dg.getArgs();
			    var operate = p.operate;
			    var userId = p.userId;
			    if(operate == 'add'){
			    	dg.addBtn("btn_confirm","确定",addOrg,"left");
			    }else if(operate == 'update'){
			    	jQuery("#pwdtr").hide();
			    	init(userId);
			    	dg.addBtn("btn_confirm","确定",addOrg,"left");
			    }else if(operate == 'show'){
			    	jQuery("#pwdtr").hide();
			    	init(userId);
			    	jQuery("input").attr("disabled","disabled");
			    	jQuery("textarea").attr("disabled","disabled");
			    }
			    
			    //确定按钮单击事件
				function addOrg(){				
					var url = operate == 'update'? "${base}/user/update":"${base}/user/add";
					var info = operate == 'update'?"修改":"添加";
					var param = {};
					param["user.userName"] = jQuery("#username").val();	
					param["user.userType"] = jQuery("#usertype").val();	
					if(operate == "add"){
						param["user.pwd"] = jQuery("#pwd").val();
						param["user.pwd1"] = jQuery("#pwd1").val();
						param["user.orgCode"] = p.orgCode;
						param["user.orgId"] = p.orgId;
					}
					param["user.fullName"] = jQuery("#fullname").val();
					param["user.sex"] = jQuery("#sex option:selected").val();
					param["user.phone"] = jQuery("#phone").val();
					param["user.mobPhone"] = jQuery("#mobphone").val();
					param["user.qq"] = jQuery("#qq").val();
					param["user.address"] = jQuery("#address").val();
					param["user.remark"] = jQuery("#remark").text();					
					var flag = validate(param);
					if(!flag){
						return;
					}
					if(operate == "add"){
						//检查用户是否已经存在
						jQuery.post("${ctx}/user/userExists",param,function(data,textStatus,jqXHR){		 
							if(data.exists){
								alert("该用户已经存在，请重新输入!");
								jQuery("#username").focus();
								return;
							}else{									
								save(url,param,info);
							}						
						},'json');	
					}else if(operate == "update"){
						param["user.id"] = userId;
						save(url,param,info);
					}								
				}
				//end 确定按钮单击事件
				
				//保存数据
				function save(url,param,info){
					jQuery.post(url,param,function(data,textStatus,jqXHR){
						if(data.success){
							alert(info + "成功!");
						}else{
							alert(info + "失败!");
						}						
						dg.cancel();
					},'json');
				}
				
				//初始化表单信息
				function init(_userId){
					var param = {};
					param.userId = _userId;
					jQuery.post("${base}/user/findUserById",param,function(data,textStatus,jqXHR){
						jQuery("#username").val(data.userName);	
						jQuery("#username").attr("readonly","readonly");
						jQuery("#fullname").val(data.fullName);	
						jQuery("#phone").val(data.phone);		
						jQuery("#mobphone").val(data.mobPhone);		
						jQuery("#qq").val(data.qq);		
						jQuery("#address").val(data.address);	
						jQuery("#remark").val(data.remark);		
						jQuery("#sex option[value='" + data.sex + "']").attr("selected","true");
						jQuery("#usertype option[value='" + data.userType + "']").attr("selected","true");			 
					},'json');									
				}				
				
				//初始化用户类型列表
				function initUserType(){
					jQuery.post("${base}/user/userType",'',function(data,textStatus,jqXHR){
						if(data != null && data.length > 0){
							var option = "";
							for(var i = 0;i < data.length;i++){
								option += "<option value='" + data[i].roleid + "'" + (i == data.length - 1 ? "selected":"") + ">" + data[i].rolename + "</option>";
							}
							jQuery("#usertype").append(option);
						}	 
					},'json');	
				}
				
				//检查页面元素输入的值是否合法
				function validate(param){
					if(param["user.userName"] == null || param["user.userName"] == ''){
						alert("用户名不能为空!");
						jQuery("#username").focus();
						return false;
					}else if(param["user.userName"].length > 10){
						alert("用户名不能超过10个字符!");
						jQuery("#username").focus();
						return false;
					}else if(!checkUserName(param["user.userName"])){
						alert("用户名只能由字母和数字组成，且必须以字母开头!");
						jQuery("#username").focus();
						return false;
					}
					if(param["user.userType"] == null || param["user.userType"] == ''){
						alert("请选择账号类型!");
						jQuery("#usertype").focus();
						return false;
					}
					if(operate == 'add'){
						if(param["user.pwd"] == null || param["user.pwd"] == ''){
							alert("密码不能为空!");
							jQuery("#pwd").focus();
							return false;
						}else if(param["user.pwd"] > 10){
							alert("密码不能超过10个字符!");
							jQuery("#pwd").focus();
							return false;
						}else if(!checkPwd(param["user.pwd"])){
							alert("密码只能由字母和数据组成!");
							jQuery("#pwd").focus();
							return false;
						}
						if(param["user.pwd1"] == null || param["user.pwd1"] == ''){
							alert("确认密码不能为空!");
							jQuery("#pwd1").focus();
							return false;
						}
						if(param["user.pwd1"] != param["user.pwd"]){
							alert("你输入的密码不一致!");
							jQuery("#pwd1").focus();
							return false;
						}
					}
					if(param["user.fullName"] == null || param["user.fullName"] == ""){						
						alert("姓名不能为空!");
						jQuery("#fullname").focus();
						return false;
					}else if(param["user.fullName"].length > 10){
						alert("姓名不能超过10个字符!");
						jQuery("#fullname").focus();
						return false;
					}
					/*
					if(param.phone.length > 15){						
						alert("联系电话不能超过15个字符!");
						jQuery("#phone").focus();
						return false;
					}
					if(param.mobPhone != null && param.mobPhone.length > 11){						
						alert("移动电话不能超过11个字符!");
						jQuery("#mobphone").focus();
						return false;
					}
					if(param.qq != null && param.qq.length > 10){						
						alert("QQ号码不能超过10个字符!");
						jQuery("#qq").focus();
						return false;
					}
					if(param.address != null && param.address.length > 50){						
						alert("联系地址不能超过50个字符!");
						jQuery("#address").focus();
						return false;
					}*/
					if(param["user.remark"] != null && param["user.remark"].length > 127){						
						alert("备注信息不能超过127个字符!");
						jQuery("#remark").focus();
						return false;
					}
					return true;
				}
				//end 检查页面元素输入的值是否合法
			});
			
			function checkUserName(val){
				var regNum =/^[A-Za-z]+[A-Za-z0-9]*$/;
				return regNum.test(val)
			}
			function checkPwd(val){
				var regNum =/^[A-Za-z0-9]+$/;
				return regNum.test(val)
			}
		</SCRIPT>
	</head>

	<body>
		<table width="99%" border="0" cellspacing="0" cellpadding="0"
			align="center">
			<tr>
				<td>
					<div class="biaoge">
						<form action="${ctx}/org/add" method="post" name="mainform" id="mainform">
							<table width="100%" border="0" cellspacing="1" cellpadding="0">
								<tr>
									<td align="left" class="biaoge_bg01" colspan="4">
										部门详细信息
									</td>
								</tr>
								<tr>
									<td width="20%" align="right" class="bg04">
										用户名：
									</td>
									<td width="30%" class="biaoge_bg5">
										<label>
											<input type="text" name="username" id="username" width="80%" maxlength="10"/><font color="red" size="4">*</font>
										</label>
									</td>
									<td  width="20%" class="bg04" align="right">账号类型：</td>
									<td width="30%" class="biaoge_bg5">
											<select name="usertype" id="usertype">
											<option value="">---请选择---</option>
											<!-- 
												<option value="1">管理员</option>
												<option value="2" selected>普通业务用户</option>
												<option value="3">查询用户</option>	
											 -->											
											</select>
									</td>									
								</tr>
								<tr id="pwdtr">
									<td width="20%" align="right" class="bg04">
										密码:
									</td>
									<td width="30%" class="biaoge_bg5">
										<label>
											<input type="password" name="pwd" id="pwd"  maxlength="10"/><font color="red" size="4">*</font>
										</label>
									</td>
									<td width="20%" align="right" class="bg04">
										确认密码：
									</td>
									<td width="30%" class="biaoge_bg5">
										<label>
											<input type="password" name="pwd1" id="pwd1"   maxlength="10"/><font color="red" size="4">*</font>
										</label>
									</td>
								</tr>
								<tr>
									<td width="20%" align="right" class="bg04">
										姓名：
									</td>
									<td width="30%" class="biaoge_bg5">
										<label>
											<input type="text" name="fullname" id="fullname"   maxlength="10"/><font color="red" size="4">*</font>
										</label>
									</td>
									<td width="20%" align="right" class="bg04">
										性别：
									</td>
									<td width="30%" class="biaoge_bg5">
										<label>
											<select name="sex" id="sex">
												<option value="1" selected>男</option>
												<option value="2">女</option>
											</select>
										</label>
									</td>
								</tr>
								<tr>
									<td width="20%" align="right" class="bg04">
										联系电话：
									</td>
									<td width="30%" class="biaoge_bg5">
										<label>
											<input type="text" name="phone" id="phone"   maxlength="15" />
										</label>
									</td>
									<td width="20%" align="right" class="bg04">
										移动电话号码：
									</td>
									<td width="30%" class="biaoge_bg5">
										<label>
											<input type="text" name="mobphone" id="mobphone"    maxlength="11"/>
										</label>
									</td>
								</tr>
								<tr>
									<td width="20%" align="right" class="bg04">
										QQ号码：
									</td>
									<td width="30%" class="biaoge_bg5">
										<label>
											<input type="text" name="qq" id="qq"  maxlength="10"/>
										</label>
									</td>
									<td width="20%" align="right" class="bg04">
										联系地址：
									</td>
									<td width="30%" class="biaoge_bg5">
										<label>
											<input type="text" name="address" id="address"   maxlength="50" />
										</label>
									</td>
								</tr>
								<tr>
									<td class="bg04" align="right">
										部门备注信息：
									</td>
									<td align="left" class="biaoge_bg5" colspan="3">
										<label>
											<textarea rows="4" cols="63" name="remark" id="remark"   maxlength="12"></textarea>
										</label>
									</td>
								</tr>
							</table>
						</form>
					</div>
				</td>
			</tr>
		</table>
</html>